//
//  TXDetailView.m
//
//  Copyright © 2019年 TXGame. All rights reserved.
//

#import "TXDetailView.h"

#define kScreenWidth  [[UIScreen mainScreen] bounds].size.width
#define kScreenHeight [[UIScreen mainScreen] bounds].size.height
#define kHPX(px)      [self heightWithPX:px]
#define kWPX(px)      [self weightWithPX:px]
#define kSize(px)     [self sizeWithPX:px]
#define kRGBColor(r,g,b) [UIColor colorWithRed:r / 255.0 green:g / 255.0 blue:b / 255.0 alpha:1.00]

@interface TXDetailView () {
    UIView *parentView;

    UITextField *tfEncryptionStr;
    UITextField *_gameIDTF;
    UITextField *_nameTF;
    UITextField *_appidTF;
    UITextField *_priceTF;
    UITextField *_gameKeyTF;
	
    UIInterfaceOrientation currentOrientation;
    CGFloat leftX;
    CGFloat rightX;
}
@end

@implementation TXDetailView

- (instancetype)initWithFrame:(CGRect)frame {
    if(self = [super initWithFrame:frame]){
        currentOrientation = [[UIApplication sharedApplication] statusBarOrientation];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(statusBarOrientationChange:) name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];

        [self initView];
    }
    return self;
}

- (void)statusBarOrientationChange:(NSNotification *)notification {
    UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
    switch (orientation) {
        case UIInterfaceOrientationPortrait:
        case UIInterfaceOrientationPortraitUpsideDown:
        case UIInterfaceOrientationLandscapeLeft:
        case UIInterfaceOrientationLandscapeRight:
            if (currentOrientation == orientation) {
                break;
            }
            currentOrientation = orientation;
            [self setFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
            [parentView removeFromSuperview];
            [self initView];
            break;
        default:
            break;
    }
}

-(void)initView {

    parentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth,kScreenHeight)];
    [parentView setBackgroundColor:[UIColor whiteColor]];

    [self addSubView:parentView width:kScreenWidth height:kScreenHeight];

    [self addSubview:parentView];
}

- (void)addSubView:(UIView *)prentView width:(int)w height:(int)h {

    UIImageView *bgImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    [bgImage setImage:[UIImage imageNamed:@"bg_image"]];
    [parentView addSubview:bgImage];

    UIView *tfView = [[UIView alloc]initWithFrame:CGRectMake((kScreenWidth - kWPX(927)) / 2, (kScreenHeight - kHPX(1205)) / 2, kWPX(927), kHPX(1005))];
    tfView.backgroundColor = [UIColor whiteColor];
    [prentView addSubview:tfView];

    CGFloat tfW = kWPX(775);
    CGFloat tfX = (kWPX(927) - tfW) / 2;
    CGFloat tfH = kHPX(117);
    // 关闭按钮
    UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    cancelBtn.frame = CGRectMake(tfX + tfW, kHPX(25), kWPX(50), kWPX(50));
    [cancelBtn setImage:[UIImage imageNamed:@"home_login_btn_close"] forState:UIControlStateNormal];
    [cancelBtn addTarget:self action:@selector(backButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    [tfView addSubview:cancelBtn];

    NSDictionary *dic = [self readGamePlist];
    if (!dic) {
        return;
    }
    
    NSArray *tfArr = @[@"游戏名称",@"游戏APPID",@"游戏ID",@"SDK接口加密字符串",@"道具价格（分）"];
    for (int i = 0; i < tfArr.count; i ++) {

        UIView *tfBaseView = [[UIView alloc]initWithFrame:CGRectMake(tfX, kHPX(107) + (tfH + kHPX(32)) * i, tfW, tfH)];
        tfBaseView.backgroundColor = kRGBColor(239, 239, 239);
        [tfView addSubview:tfBaseView];

        UITextField *tf = [[UITextField alloc]initWithFrame:CGRectMake(kWPX(30), 0, tfW - kWPX(30), tfH)];
        tf.placeholder = tfArr[i];
        tf.layer.cornerRadius  = kWPX(6);
        tf.layer.masksToBounds = YES;
        tf.textColor = kRGBColor(51, 51, 51);
        tf.font = [UIFont systemFontOfSize:kSize(36)];
        [tfBaseView addSubview:tf];
        
        if (i == 0) {
            // 1、游戏名称
            tf.text = [NSString stringWithFormat:@"%@",[dic objectForKey:@"game_name"]];
            _nameTF = tf;
        }else if (i == 1) {
            // 2、游戏APPID
            tf.text = [NSString stringWithFormat:@"%@",[dic objectForKey:@"game_appid"]];
            _appidTF = tf;
        }else if (i == 2) {
            // 3、游戏ID
            tf.text = [NSString stringWithFormat:@"%@",[dic objectForKey:@"game_id"]];
            _gameIDTF = tf;
        }else if (i == 3) {
            // 4、SDK接口加密字符串
            tf.text = [NSString stringWithFormat:@"%@",[dic objectForKey:@"access_key"]];
            _gameKeyTF = tf;
        }else if (i == 4) {
            // 5、道具价格（分）
//            tf.text = [[TXAccountInfo sharedInstance] getPrice];
            _priceTF = tf;
        }
    }
}

- (void)backButtonClicked {
    NSString *priceStr = [_priceTF text];
    if (![priceStr isEqualToString:@""]) {
        if (self.delegate && [self.delegate respondsToSelector:@selector(returnPopsPrice:)]) {
            [self.delegate returnPopsPrice:priceStr];
        }
    }
    [self removeFromSuperview];
}

- (NSDictionary *)readGamePlist {
    NSString *filename = @"txgameinfo";
    NSBundle *libBundle = [NSBundle bundleWithPath:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"TxyxSDK.bundle"]];
    NSString *path1 = @"";
    if(libBundle && filename){
        path1 = [libBundle pathForResource:filename ofType:@".plist"];
    }
    NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path1];
    return dict;
}

/** 设置PX与iOS尺寸高度的转换 */
- (CGFloat)heightWithPX:(int)px {
    CGSize size = [[UIScreen mainScreen] bounds].size;
    CGFloat height = px * size.height / (736.0 * 3);
    if(size.width > size.height) {
        // 横屏
        height = px * size.height / (414.0 * 3);
    }
    return height;
}

/** 设置PX与iOS尺寸宽度的转换 */
- (CGFloat)weightWithPX:(int)px {
    CGSize size = [[UIScreen mainScreen] bounds].size;
    CGFloat weight = px * size.width / (414.0 * 3);
    if(size.width > size.height) {
        // 横屏
        weight = px * size.width / (736.0 * 3);
    }
    return weight;
}

/** 字体大小与UI给的px对应关系 */
- (CGFloat)sizeWithPX:(int)px {
    CGSize size = [[UIScreen mainScreen] bounds].size;
    CGFloat fontSize = px * size.width / (414.0 * 2);
    if(size.width > size.height) {
        // 横屏
        fontSize = px * size.width / (736.0 * 2);
    }
    //    CGFloat size = px *  [UIScreen mainScreen].bounds.size.width / (414.0*3);
    return fontSize;
}

@end
